import React from "react";
import { Button, Checkbox, Form, Input, message } from "antd";
import "./less/login.less";
import logo from "./logo.png";
import { UserOutlined, UnlockOutlined } from "@ant-design/icons";
import { Link, useNavigate } from "react-router-dom";
import { RegisterApi } from "./request/api";

export default function Register() {
  const navigate = useNavigate();
  const onFinish = (values) => {
    console.log("Success:", values);
    RegisterApi({
      username: values.username,
      password: values.password,
    }).then((res) => {
      if (res.errCode === 0) {
        setTimeout(() => {
          navigate("/login");
        }, 1000);
      } else {
        message.error(res.message);
      }
    });
  };

  // const onFinishFailed = (errorInfo) => {
  //   console.log("Failed:", errorInfo);
  // };
  return (
    <div className="login">
      <div className="login-box">
        <img src={logo} alt="" />
        <Form
          name="basic"
          onFinish={onFinish}
          // onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            name="username"
            rules={[
              {
                required: true,
                message: "Please input your username!",
              },
            ]}
          >
            <Input
              placeholder="请输入用户名"
              size="large"
              prefix={<UserOutlined />}
            />
          </Form.Item>

          <Form.Item
            name="password"
            rules={[
              {
                required: true,
                message: "Please input your password!",
              },
            ]}
          >
            <Input.Password
              placeholder="请输入密码"
              size="large"
              prefix={<UnlockOutlined />}
            />
          </Form.Item>
          <Form.Item
            name="confirm"
            dependencies={["password"]}
            hasFeedback
            rules={[
              {
                required: true,
                message: "Please confirm your password!",
              },
              ({ getFieldValue }) => ({
                validator(_, value) {
                  if (!value || getFieldValue("password") === value) {
                    return Promise.resolve();
                  }
                  return Promise.reject(
                    new Error(
                      "The two passwords that you entered do not match!"
                    )
                  );
                },
              }),
            ]}
          >
            <Input.Password
              placeholder="请再次·输入密码"
              size="large"
              prefix={<UnlockOutlined />}
            />
          </Form.Item>
          <Form.Item>
            <Link to="/login">已有账号？去登录</Link>
          </Form.Item>
          <Form.Item>
            <Button type="primary" htmlType="submit" size="large" block>
              立即注册
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}
